import pymysql


class MySql:
    # conda install conda-forge::pymysql
    @staticmethod
    def insert(database: dict, sql: str, values: list):
        """
        导入数据库
        :param database: 数据库配置
        :param sql:
        :param values:
        :return:
        """
        conn = pymysql.connect(
            host=database['host'],
            user=database['user'],
            password=database['password'],
            db=database['db'],
            port=database['port']
        )
        cur = conn.cursor()  # 获取操作游标，也就是开始操作
        cur.executemany(sql, values)
        conn.commit()
        conn.close()
        print('finished ', len(values))

    @staticmethod
    def get_data(database: dict, sql: str, values: list = None):
        """
        获取数据
        :param database: 数据库配置
        :param sql:
        :param values:
        :return:
        """
        conn = pymysql.connect(
            host=database['host'],
            user=database['user'],
            password=database['password'],
            db=database['db'],
            port=database['port'],
            cursorclass=pymysql.cursors.DictCursor  # 返回字典格式的结果
        )
        cur = conn.cursor()  # 获取操作游标，也就是开始操作
        if values and len(values) > 0:
            cur.execute(sql, values)
        else:
            cur.execute(sql)
        result = cur.fetchall()
        conn.commit()
        conn.close()
        return result
